package com.oncliedisk.administrator.utils;

import android.content.Context;
import android.os.Environment;
import android.widget.Toast;

import com.oncliedisk.administrator.model.LocalFileDirInfo;

import java.io.File;
import java.io.FileFilter;
import java.math.BigDecimal;
import java.text.Collator;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.List;


public class ExploerLoadSdFileTask {
	public static ArrayList<LocalFileDirInfo> loadSdFile(File file) {
		ArrayList<LocalFileDirInfo> list = new ArrayList<LocalFileDirInfo>();
		FileFilter fileFilter = new FileFilter() {
			@Override
			public boolean accept(File pathname) {

				if (!pathname.canRead()) {
					return false;
				}

				return true;
			}
		};

		ArrayList<LocalFileDirInfo> dirList = new ArrayList<LocalFileDirInfo>();
		ArrayList<LocalFileDirInfo> fileList = new ArrayList<LocalFileDirInfo>();
		if(file != null){
			File[] files = file.listFiles(fileFilter);
			for (File f : files) {
				LocalFileDirInfo info = new LocalFileDirInfo();
				info.file = f;
				info.name = f.getName();
				info.ctime = getFormateTime(new Date(f.lastModified()));
				if (f.isDirectory()) {
					info.isFile = false;
					File[] childFiles = f.listFiles(fileFilter);
					if (childFiles == null) {
						continue;
					} else {
						info.fileNum = childFiles.length;
					}
					dirList.add(info);
				} else {
					info.isFile = true;
					info.filesize = formateFileSize(f.length());
					fileList.add(info);
				}
			}
			list.addAll(orderFileListByName(dirList));
			list.addAll(orderFileListByName(fileList));
		}
		return list;
		
	}
	public static String getFormateTime(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}

	public static String formateFileSize(double filesize) {
		double kiloByte = filesize / 1024;
		if (kiloByte < 1) {
			return filesize + " B";
		}
		double megaByte = kiloByte / 1024;
		if (megaByte < 1) {
			BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
			return result1.setScale(2, BigDecimal.ROUND_HALF_UP)
					.toPlainString() + " KB";
		}
		double gigaByte = megaByte / 1024;
		if (gigaByte < 1) {
			BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
			return result2.setScale(2, BigDecimal.ROUND_HALF_UP)
					.toPlainString() + " MB";
		}
		double teraBytes = gigaByte / 1024;
		if (teraBytes < 1) {
			BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
			return result3.setScale(2, BigDecimal.ROUND_HALF_UP)
					.toPlainString() + " GB";
		}
		double petaBytes = teraBytes / 1024;
		if (petaBytes < 1) {
			BigDecimal result4 = new BigDecimal(Double.toString(teraBytes));
			return result4.setScale(2, BigDecimal.ROUND_HALF_UP)
					.toPlainString() + " TB";
		}

		double exaBytes = petaBytes / 1024;
		if (exaBytes < 1) {
			BigDecimal result5 = new BigDecimal(Double.toString(petaBytes));
			return result5.setScale(2, BigDecimal.ROUND_HALF_UP)
					.toPlainString() + " PB";
		}

		double zettaBytes = exaBytes / 1024;
		if (zettaBytes < 1) {
			BigDecimal result6 = new BigDecimal(Double.toString(exaBytes));
			return result6.setScale(2, BigDecimal.ROUND_HALF_UP)
					.toPlainString() + " EB";
		}
		double yottaBytes = zettaBytes / 1024;
		if (yottaBytes < 1) {
			BigDecimal result7 = new BigDecimal(Double.toString(zettaBytes));
			return result7.setScale(2, BigDecimal.ROUND_HALF_UP)
					.toPlainString() + " ZB";
		}
		BigDecimal result8 = new BigDecimal(yottaBytes);
		return result8.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()
				+ " YB";
	}
	public static List<LocalFileDirInfo> orderFileListByName(
			ArrayList<LocalFileDirInfo> orgList) {
		LocalFileDirInfo[] arr = orgList.toArray(new LocalFileDirInfo[orgList
				.size()]);
		Arrays.sort(arr, new NameComparator<LocalFileDirInfo>());
		return Arrays.asList(arr);
	}

	public static class NameComparator<T> implements Comparator<Object> {

		@Override
		public int compare(Object object1, Object object2) {
			if (object1 instanceof LocalFileDirInfo
					&& object2 instanceof LocalFileDirInfo) {
				String name1 = ((LocalFileDirInfo) object1).name;
				String name2 = ((LocalFileDirInfo) object2).name;
				Comparator cmp = Collator.getInstance(java.util.Locale.CHINA);
				return cmp.compare(name1, name2);

			}
			return Integer.MIN_VALUE;
		}
	}
	public static boolean isMountSdCard(Context ctx) {
		return Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED);
	}
	public static void showToast(Context ctx, String msg, int duration) {
		Toast mToast = Toast.makeText(ctx, msg, duration);
		mToast.show();
	}

}
